package Controller;



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import Database.Database;
import java.io.IOException;
import java.io.PrintWriter;
import static java.rmi.Naming.list;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static java.util.Collections.list;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kojar
 */
public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("step0");
        Database db = new Database();
        // get request parameters for userID and password
        String user =  request.getParameter("Username");
        String pwd = request.getParameter("Password");    
        System.out.println("step1");
        try {
            Object[] LoginDetail = db.cekLogin(user, pwd).toArray();
            System.out.println("step2");
            if (LoginDetail.length == 3) {
                HttpSession session = request.getSession();
                String User = LoginDetail[0].toString();
                String Privilege = LoginDetail[2].toString();
                //System.out.println(User);
                //System.out.println(Privilege);   
                session.setAttribute("user", User);
                session.setMaxInactiveInterval(30 * 60);
                Cookie username = new Cookie("user", User);
                Cookie privilege = new Cookie("privilege", Privilege);
                username.setMaxAge(30 * 60);
                privilege.setMaxAge(30 * 60);
                response.addCookie(username);
                response.addCookie(privilege);
                request.getRequestDispatcher("/WEB-INF/UserView/UserMenu.jsp").forward(request, response);
            } else {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
                PrintWriter out = response.getWriter();
                out.println("<font color=red>Either user name or password is wrong.</font>");
                rd.include(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
